#include	"Wiimote4Virtools.h"
#include	"WiimRumble.h"

// http://digitalretrograde.com/projects/wiim/

CKObjectDeclaration *FillBehaviorWiimRumble()
{
    CKObjectDeclaration *od = CreateCKObjectDeclaration ( "WiimoteRumble" );
    od->SetDescription ( "Rumbles the Wiimote." );
    od->SetCategory ( "Wiimote" );
    od->SetType ( CKDLL_BEHAVIORPROTOTYPE );
    od->SetGuid ( WIIMRUMBLE_GUID );
    od->SetAuthorGuid ( AUTHOR_GUID );
    od->SetAuthorName ( "Jonathan DERROUGH" );
    od->SetVersion ( 0x00000001 );
    od->SetCreationFunction ( CreateWiimRumbleProto );
    //od->SetCompatibleClassId ( CKCID_3DENTITY );
    return od;
}

CKERROR CreateWiimRumbleProto ( CKBehaviorPrototype **pproto)
{
    CKBehaviorPrototype *proto = CreateCKBehaviorPrototype("WiimRumble");

    if(!proto)
		return (CKERR_OUTOFMEMORY);

    proto->DeclareInput("In");
    proto->DeclareOutput("Out");
		
    proto->DeclareInParameter("Wiimote id", CKPGUID_INT );
    proto->DeclareInParameter("State", CKPGUID_BOOL );

    proto->SetFlags(CK_BEHAVIORPROTOTYPE_NORMAL);
    proto->SetFunction(WiimRumble);
    //proto->SetBehaviorFlags(CKBEHAVIOR_TARGETABLE);
    
    *pproto = proto;
    return (CK_OK);
}

int WiimRumble(const CKBehaviorContext& behcontext)
{
	// Wiimote(s) initialized ?
	if (g_wiimotes.size() < 1)
		return (CKBR_GENERICERROR);

	CKBehavior* beh = behcontext.Behavior;
	
	// Get the id of the Wiimote we want to rumble
	int wiimote_id = 0;
	beh->GetInputParameterValue(0, &wiimote_id);

	// Get the state (on / off)
	CKBOOL state;
	beh->GetInputParameterValue(1, &state);

	// Strength of rumble ?
	/*
	bool rumble_strength = false;
	beh->GetInputParameterValue(1, &rumble_strength);
	*/
	g_wiimotes[wiimote_id].SetRumble(state == FALSE ? false : true);

	beh->ActivateOutput(0);

	return (CK_OK);
}